/* LinkedList -  This module is responsible for a generic linked list */

#include "stdafx.h"
#include "LinkedList.h"


/*********************************************************************************************************************** */
/* Description: Initialize the linked list */
/* Input/Output: list - List to initialize
/*********************************************************************************************************************** */
void InitList(pLinkedList list)
{
	list->head = NULL;
	list->tail = NULL;
}

/*********************************************************************************************************************** */
/* Description: Insert a node to the linked list */
/* Input/Output: list - List to update */
/* Input:		 data - data of the element */
/*********************************************************************************************************************** */
void Insert(pLinkedList list, void* data)
{
	pElement element = (Element*)malloc(sizeof(Element));
	element->data = data;
	element->Next = NULL;

	/* Handle empty list */
	if (list->head == NULL)
	{
		list->head = element;
		list->head->Next = NULL;
		list->tail = list->head;
		list->head->Previous = NULL;
	}
	else
	{
		list->tail->Next = element;
		element->Previous = list->tail;
		
		list->tail = element;
	}
}

/*********************************************************************************************************************** */
/* Description: Check if data exists in the list */
/* Input/Output: list - List to search */
/* Input:		 data - data to find */
/*********************************************************************************************************************** */
BOOL ContainsData(pLinkedList list, void* data)
{
	return Search(list, data) != NULL;
}

/*********************************************************************************************************************** */
/* Description: Search  element in the list */
/* Input/Output: list - List to search */
/* Input:		 data - data to find */
/*********************************************************************************************************************** */
pElement Search(pLinkedList list, void* data)
{
	pElement head = list->head;
	while (head != NULL)
	{
		if (list->Equal(data, head->data) == TRUE)
			return head;
		
		head = head->Next;
	}

	return NULL;
}

/*********************************************************************************************************************** */
/* Description: Frees all the memory of the linked list */
/* Input/Output: list - List to search */
/*********************************************************************************************************************** */
void DeleteList(pLinkedList list)
{
	pElement head = list->head;
	pElement next;
	while (head != NULL)
	{
		next = head->Next;
		delete head;
		head = next;
	}
}

pElement Remove(pLinkedList list, pElement current)
{
	if (!current)
		return NULL;

	/* Remove head of the list */
	if  (!current->Previous) 
	{
		list->head = current->Next;

		/* There is in the list only one item */
		if (!list->head)
			list->tail = NULL;
		/* There are several items in the list */
		else
			list->head->Previous = NULL;
	}

	/* Remove tail of the list */
	else if  (!current->Next) 
	{
		list->tail = current->Previous;
		list->tail->Next = NULL;
	}
	/* Remove element which is not the tail nor the head */
	else
	{
		(current->Next)->Previous=current->Previous;	
		(current->Previous)->Next=current->Next;	
	}

	pElement next=current->Next;
	free(current);
	return next;
}

/*********************************************************************************************************************** */
/* Description: Compares two strings */
/* Input/Output: str1 - first string */
/* Input:		 str2 - second string */
/*********************************************************************************************************************** */
BOOL EqualString(void* str1, void* str2)
{
	if (strcmp((char*)str1, (char*)str2) == 0)
		return TRUE;
	return FALSE;
}
